/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uvexposureapplication.rpc.servers;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import uvexposureapplication.rpc.AbstractPseudoRPCServerStub;
import uvexposureapplication.server.LocationServer;

/**
 *
 * @author matthieukluj
 */
public class LocationServerPseudoRPCServerStub extends AbstractPseudoRPCServerStub {

    LocationServer lServer;

    public LocationServerPseudoRPCServerStub(LocationServer lServer, String methodName, String server) {
        super(methodName, server);
        this.lServer = lServer;
        this.serverName = "LocationServer";
    }

    @Override
    public void produce(String parameters) {
        Map<String, String> notification = new HashMap<String, String>();
        notification.put("RPCServerName", this.serverName);
        notification.put("Side", "Server");
        notification.put("MethodName", this.methodName);
        if (this.methodName.equals("shutDown")) {
            this.lServer.shutDown();
        } else {
            notification.put("Result", lServer.fetchIndoorLocation());
        }
        //System.out.println("Send by LocationServer STub: " + lServer.fetchIndoorLocation());
        this.producer.produceFromCollection(notification);
    }

    @Override
    public void subscribeToServer() {
        this.consumer.subscribe("RPCServerName == '" + this.serverName + "' && "
                + "MethodName == '" + this.methodName + "'");
    }

    @Override
    public void update(Observable o, Object arg) {
        if (arg instanceof Map) {
            Map<String, Object> argMap = (Map<String, Object>) arg;

            if (argMap.get("RPCServerName").equals(this.serverName)) {
                if (argMap.get("MethodName").equals(this.methodName)) {
                    if (argMap.get("Side").equals("Client")) {
                        this.produce(null);
                    }
                }
            }
        }
    }
}
